package com.killer.xialaxuanze;

import android.graphics.drawable.BitmapDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {

    private ListView mListView;
    private EditText mEditText;
    private List<String> datas;
    private PopupWindow mPopupWindow;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mEditText = (EditText) findViewById(R.id.et_input);
    }

    public void ClickIB(View view) {
        showPopWindow();
    }

    private void showPopWindow() {
        initListView();
        //显示下拉选择框
        mPopupWindow = new PopupWindow(mListView,mEditText.getWidth(),300);

        //设置点击外部区域，自动隐藏
        mPopupWindow.setOutsideTouchable(true);//设置外部可触摸
        mPopupWindow.setBackgroundDrawable(new BitmapDrawable());//设置空背景，响应点击事件
        mPopupWindow.setFocusable(true);//设置可获取焦点

        //显示在指定控件下
        mPopupWindow.showAsDropDown(mEditText,0,-5);
    }

    //初始化要
    private void initListView() {
        mListView = new ListView(this);
        //mListView.setDividerHeight(0);//取消分割线
        mListView.setBackgroundResource(R.drawable.listview_background);
        mListView.setOnItemClickListener(this);

        datas = new ArrayList<>();
        //虚拟一些数据
        for (int i=0;i<30;i++){
            datas.add(10000+i+"");
        }
        mListView.setAdapter(new MyAdapter());
    }



    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String s = datas.get(position);
        mEditText.setText(s);
        mPopupWindow.dismiss();

    }
    class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return datas.size();
        }

        @Override
        public Object getItem(int position) {
            return datas.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            View view;
            if (convertView == null){
                view = View.inflate(parent.getContext(),R.layout.item_number,null);
            }else {
                view = convertView;
            }
            TextView tv_number = (TextView) view.findViewById(R.id.tv_number);
            tv_number.setText(datas.get(position));

            view.findViewById(R.id.ib_delete).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    datas.remove(position);
                    notifyDataSetChanged();
                    if (datas.size()==0){
                        //如果删除的是最后一条，隐藏popupwindow;
                        mPopupWindow.dismiss();
                    }
                }
            });


            return view;
        }
    }
}
